define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Starbusiness_publicity_apply, $timeout, GoDialog, Auth, GoPage, GoPicture, $stateParams,$ionicPopup) {
        //初始化输入
        $scope.input = new Object();
        $scope.input.video_url = ''; //视频地址
        $scope.input.title = ''; //视频名称
        $scope.input.video_type = "";//视频类型
        $scope.input.video_select = { id: 0, name: "请选择视频类型" };
        $scope.input.video_items = [{id:"company",name:"明星企业"},{id:"type_1",name:"公司展示"},{id:"type_2",name:"加盟店开业"},{id:"type_3",name:"展会视频"},{id:"type_4",name:"会议剪彩"}];
        $scope.input.company_profile = ''; //企业简介
        $scope.input.image = new Object(); //视频缩略图
        $scope.input.id = $stateParams.id;
        $scope.title="申请";//html的标题

        if($stateParams.id){
            $scope.title="编辑";
        }
		$scope.doSelectVideo = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.input.video_items,$scope.input.video_select,function(data){
                $scope.input.video_type = data.id;
                $scope.input.video_select = data;
			});
		}
        $scope.init_data = function() {
            Starbusiness_publicity_apply.get_vedio($scope.input.id, function(data) {
            	console.log(data);
                $scope.input=data;
                $scope.image=data.image.image;
            });
        }
        if($scope.input.id) {
            $scope.init_data(); //初始化
        }
        //动态响应
        // 上传图片
		$scope.addStoreImg = function() {
			GoPicture.upload(function(res) {
				if(!res.success) {
					GoDialog.toast(res.msg);
					return;
				}
				if($scope.input.image.image == null){
					var r = res.result;
					$scope.input.image = new Object();
					$scope.input.image.id = r.image_id;
					$scope.input.image.image = r.image_src;
					GoDialog.toast("添加成功！");
				}else{
					GoDialog.toast("只能上传一张图片！");
				}
				
				//GoDialog.toast($scope.input.image.id);
			});
		}
		
		$scope.openFileActSheet = function() {
			var src = $scope.input.image.image;

			var buttons = new Array();
			buttons.push({ text: '删除图片', style: '' });
			buttons.push({ text: '显示大图', style: '' });

			GoDialog.sheet('图片操作', '取消', buttons, function(bindex) {

				if(bindex == 0) {
					GoDialog.confirm('删除图片', '是否要删除该图片？', function() {
						$timeout(function() {
							$scope.sheetActionRemove();
						}, 1);
					})
				} else if(bindex == 1) {
					}

				return true;
			});
		}

		$scope.sheetActionRemove = function() {
			var s = $scope.input.image;
			s.id = null;
			s.image = null;

		}
        //保存数据
        $scope.save = function() {
            if(!$scope.input.company_profile) {
                GoDialog.toast("请输入企业简介！");
            } else {
                Starbusiness_publicity_apply.edit($scope.input.id,$scope.input.title, $scope.input.video_url, $scope.input.image, $scope.input.company_profile, $scope.input.video_type, function(data) {
                    if(data.success) {
                        GoDialog.toast("提交数据成功！");
                        GoPage.go_states("starbusiness_publicity");
                    } else {
                        GoDialog.toast(data.msg);
                    }
                })
            }
        };


    }

    controller.$inject = ['$scope', 'Starbusiness_publicity_apply', '$timeout', 'GoDialog', 'Auth', 'GoPage', 'GoPicture', '$stateParams','$ionicPopup'];

    return app.registerController("starbusiness_publicity_applyctrl",controller);
});